#pragma once
#include <iostream>
#include <string>
#include <cstdarg>
#include <cstdio>
#include <cstring>

#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>


#define Info 0
#define Debug 1
#define Warning 2
#define Error 3
#define Fatal 4

#define Screan 1
#define Onefile 2
#define Classfile 3

#define SIZE 1024
#define LOGFILE "log.txt"


class Log
{
public:
    Log(int _outmethod = Screan)
        :_printMethod(_outmethod) ,_path("./log/")
    {}
    void outMethod(int method)
    {
        _printMethod = method;
    }
    void operator()(int level, const char* format, ...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftbuffer[SIZE];
        snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
                ctime->tm_year+1900, ctime->tm_mon+1, ctime->tm_mday,
                ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        
        va_list s;
        va_start(s, format);
        char rightbuffer[SIZE];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
        va_end(s);

        char logtxt[SIZE*2];
        snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);
        
        printLog(level, logtxt);
    }
    ~Log()
    {}
private:
    std::string levelToString(int level)
    {
        switch (level)
        {
            case Info:    return "Info";
            case Debug:   return "Debug";
            case Warning:  return "Warning";
            case Error:    return "Error";
            case Fatal:    return "Fatal";
            default:      return "None";
        }
    }
    void printLog(int level, const std::string &logtxt)
    {
        switch (_printMethod)
        {
            case Screan:     std::cout << logtxt << std::endl;  break;
            case Onefile:    printOnefile(LOGFILE, logtxt);  break;
            case Classfile:  printClassfile(level, logtxt);  break;
            default: break;
        }
    }
    void printOnefile(const std::string &logname, const std::string &logtxt)
    {   
        char cwd[SIZE];
        getcwd(cwd, sizeof(cwd));
        // 检测存放logtxt的路径是否为当前工作目录
        if (_path != "./" && _path != std::string(cwd))
        {
            mkdir(_path.c_str(),0777);
        }
        std::string filename = _path + logname;
        int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0666);
        if (fd < 0)
        {
            perror("openLogfile fail, errno string");
            return;
        }
        write(fd, logtxt.c_str(), logtxt.size());
        const char newline = '\n';
        write(fd, &newline, sizeof(char));
        close(fd);
    }
    void printClassfile(int level, const std::string &logtxt)
    {
        std::string filename = LOGFILE;
        filename += "." + levelToString(level);
        printOnefile(filename, logtxt);
    }

private:
    int _printMethod;
    std::string _path;
};

    // void logMessage(int level, const char* format, ...)
    // {
    //     time_t t = time(nullptr);
    //     struct tm *ctime = localtime(&t);
    //     char leftbuffer[SIZE];
    //     snprintf(leftbuffer, sizeof(leftbuffer), "[%s][%d-%d-%d %d:%d:%d]", levelToString(level).c_str(),
    //             ctime->tm_year+1900, ctime->tm_mon+1, ctime->tm_mday,
    //             ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        
    //     va_list s;
    //     va_start(s, format);
    //     char rightbuffer[SIZE];
    //     vsnprintf(rightbuffer, sizeof(rightbuffer), format, s);
    //     va_end(s);

    //     char logtxt[SIZE*2];
    //     snprintf(logtxt, sizeof(logtxt), "%s %s", leftbuffer, rightbuffer);
    //     std::cout << logtxt << std::endl;
    // }